package com.eden.market.geoedu.view;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.util.Log;

import com.eden.market.geoedu.GameIdConstants;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

public class MapPushPinOverlay extends ItemizedOverlay<OverlayItem> {
	private List<OverlayItem> items = new ArrayList<OverlayItem>();
	private Paint innerPaint, borderPaint, textPaint;
	private GeoPoint geoPoint;
	private String colorMarker;

	public MapPushPinOverlay(Drawable marker, GeoPoint point, String color) {
		super(boundCenterBottom(marker));
		geoPoint = point;
		colorMarker = color;
		addItem(new OverlayItem(geoPoint, "test", "test2"));
		populate();
	}

	@Override
	public boolean onTap(int index) {
		Log.e("MapViewDemo", "ONTAP index : " + index + " place : ");
		return super.onTap(index);
	}

	@Override
	public void draw(Canvas canvas, MapView myMapView, boolean shadow) {
		super.draw(canvas, myMapView, shadow);
		// Log.e("MapViewDemo", "MapDemoOverlay...draw");
		// Setup our "brush"/"pencil"/ whatever...
		Paint paint = new Paint();
		paint.setTextSize(12);

		// Create a Point that represents our GPS-Location
		// Double lat = MapViewDemo.this.myLocation.getLatitude() * 1E6;
		// Double lng = MapViewDemo.this.myLocation.getLongitude() * 1E6;
		// GeoPoint geoPoint = new GeoPoint(lat.intValue(), lng.intValue());
		Projection myprojection = myMapView.getProjection();
		Point point = new Point();
		myprojection.toPixels(geoPoint, point);
		// Setup a color for our location

		paint.setStyle(Style.FILL);

		if (colorMarker.equals(GameIdConstants.BLUE)) {
			paint.setARGB(255, 56, 131, 225); // Nice blue
		}
		else {
			paint.setARGB(255, 255, 0, 17); // Nice red
		}
		// Draw our name
		// canvas.drawText(getString(R.string.map_overlay_own_name),
		// point.x + 9, point.y, paint);
		// paint.setStrokeWidth(1);
		// paint.setARGB(255, 0, 0, 0);
		canvas.drawCircle(point.x, point.y, 2, paint);
		// Log.e("MapViewDemo", "MapDemoOverlay...draw
		// paint.setStyle(Style.FILL);
		// drawInfoWindow(canvas, myprojection, shadow);
		// ArrayList<Placemark> placeMarks = MapViewDemo.this.getSearch();
		// Placemark placeMark;
		// if (placeMarks != null)
		// synchronized (placeMarks) {
		// for (int it = 0; it < placeMarks.size(); it++) {
		// placeMark = (Placemark) placeMarks.get(it);
		// geoPoint = placeMark.getPoint();
		// myprojection.toPixels(geoPoint, point);
		// canvas.drawText(placeMark.getTitle(), point.x + 9,
		// point.y, paint);
		// }
		// }
	}

	public void addItem(OverlayItem item) {
		items.add(item);
		populate();
	}

	@Override
	protected OverlayItem createItem(int index) {
		return items.get(index);
	}

	@Override
	public int size() {
		return items.size();
	}

	private void drawInfoWindow(Canvas canvas, Projection myprojection,
			boolean shadow) {

		/*
		 * if (MapViewDemo.this.selectedSearchPoint != null) { GeoPoint geoPoint
		 * = MapViewDemo.this.selectedSearchPoint .getPoint(); Point point = new
		 * Point(); myprojection.toPixels(geoPoint, point);
		 * 
		 * // Setup the info window with the right size & location int
		 * INFO_WINDOW_WIDTH = 150; int INFO_WINDOW_HEIGHT = 30; RectF
		 * infoWindowRect = new RectF(0, 0, INFO_WINDOW_WIDTH,
		 * INFO_WINDOW_HEIGHT); int infoWindowOffsetX = point.x -
		 * INFO_WINDOW_WIDTH / 2; int infoWindowOffsetY = point.y -
		 * INFO_WINDOW_HEIGHT - bubbleIcon.getHeight();
		 * infoWindowRect.offset(infoWindowOffsetX, infoWindowOffsetY);
		 * 
		 * // Draw inner info window canvas.drawRoundRect(infoWindowRect, 5, 5,
		 * getInnerPaint()); // Draw border for info window
		 * canvas.drawRoundRect(infoWindowRect, 5, 5, getBorderPaint()); // Draw
		 * the MapLocation's name int TEXT_OFFSET_X = 10; int TEXT_OFFSET_Y =
		 * 15; Log.e("Glocation", " selectedSearchPoint5 " +
		 * MapViewDemo.this.selectedSearchPoint);
		 * canvas.drawText(MapViewDemo.this.selectedSearchPoint .getSnippet(),
		 * infoWindowOffsetX + TEXT_OFFSET_X, infoWindowOffsetY + TEXT_OFFSET_Y,
		 * getTextPaint()); }
		 */
	}

	public Paint getInnerPaint() {
		if (innerPaint == null) {
			innerPaint = new Paint();
			innerPaint.setARGB(225, 75, 75, 75); // gray
			innerPaint.setAntiAlias(true);
		}
		return innerPaint;
	}

	public Paint getBorderPaint() {
		if (borderPaint == null) {
			borderPaint = new Paint();
			borderPaint.setARGB(255, 255, 255, 255);
			borderPaint.setAntiAlias(true);
			borderPaint.setStyle(Style.STROKE);
			borderPaint.setStrokeWidth(2);
		}
		return borderPaint;
	}

	public Paint getTextPaint() {
		if (textPaint == null) {
			textPaint = new Paint();
			textPaint.setARGB(255, 255, 255, 255);
			textPaint.setAntiAlias(true);
		}
		return textPaint;
	}
}